package me.firstapp.spi.bbs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonCollection;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.bbs.CollectionService;

@Service
public class CollectionServiceImpl extends BaseServiceImpl implements CollectionService {

	@Override
	public SingleObject<Long> findCollectionCounts(Long memberId, Long topicId, Long productId) {
		SingleObject<Long> result = new SingleObject<Long>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("topicId", topicId);
			parameters.put("productId", productId);
			JSONObject resultObject = api(parameters, "/collection/findCollectionCounts.do", HttpMethod.GET, "查询收藏数量");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				Long counts = resultObject.getLong("object");
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(counts);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;

	}

	@Override
	public SingleObject<JsonCollection> findMemberTopicCollectionRel(Long memberId, Long topicId) {
		SingleObject<JsonCollection> result = new SingleObject<JsonCollection>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("topicId", topicId);
			JSONObject resultObject = api(parameters, "/collection/findMemberTopicCollectionRel.do", HttpMethod.GET,
					"查询用户话题收藏关系");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject jsonObject = resultObject.getJSONObject("object");
				JsonCollection collection = JSONObject.toJavaObject(jsonObject, JsonCollection.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(collection);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Boolean> changCollectionStatus(Long memberId, Long topicId, Long productId, Integer type,
			Boolean isCollection) {
		SingleObject<Boolean> result = new SingleObject<Boolean>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("topicId", topicId);
			parameters.put("productId", productId);
			parameters.put("type", type);
			parameters.put("isCollection", isCollection);

			JSONObject resultObject = api(parameters, "/collection/changCollectionStatus.do", HttpMethod.POST,
					"更改用户收藏状态");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(resultObject.getBooleanValue("object"));
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;

	}

	@Override
	public PageObject<JsonCollection> findMemberCollections(Integer pageNo, Integer pageSize, Long memberId,
			Integer type) {
		PageObject<JsonCollection> result = new PageObject<JsonCollection>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("memberId", memberId);
			parameters.put("type", type);

			JSONObject resultObject = api(parameters, "/collection/findMemberCollections.do", HttpMethod.GET,
					"分页查询用户收藏");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject pageJsonObject = resultObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonCollection> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonCollection> collectionList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonCollection collection = JSONObject.toJavaObject(_jsonObject, JsonCollection.class);
								collectionList.add(collection);
							}
						}
					}
					jsonPage.setList(collectionList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;

	}

	@Override
	public SingleObject<JsonCollection> findMemberProductCollectionRel(Long memberId, Long productId) {
		SingleObject<JsonCollection> result = new SingleObject<JsonCollection>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("productId", productId);
			JSONObject resultObject = api(parameters, "/collection/findMemberTopicCollectionRel.do", HttpMethod.GET,
					"查询用户话题收藏关系");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject jsonObject = resultObject.getJSONObject("object");
				JsonCollection collection = JSONObject.toJavaObject(jsonObject, JsonCollection.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(collection);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Long> findCollectionCounts(Long memberId, Long topicId, Long productId, Long apiClientId) {
		SingleObject<Long> result = new SingleObject<Long>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("topicId", topicId);
			parameters.put("productId", productId);
			parameters.put("apiClientId", apiClientId);

			JSONObject resultObject = api(parameters, "/batch/findCollectionCounts.do", HttpMethod.GET, "查询收藏数量");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				Long counts = resultObject.getLong("object");
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(counts);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}
}
